package com.zjdiepu.www.rest.api.third;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.ibiz.third.IJuheCityService;
import com.zjdiepu.www.ibiz.third.IJuheProvinceService;
import com.zjdiepu.www.model.third.dto.JuheCity;
import com.zjdiepu.www.model.third.dto.JuheProvince;
import com.zjdiepu.www.model.third.ext.JuheCityExt;
import com.zjdiepu.www.model.third.ext.JuheProvinceExt;
import com.zjdiepu.www.model.third.request.RegionRequest;
import com.zjdiepu.www.model.third.response.RegionResponse;

/**
 * 地区搜索Controller层<br/>
 * 
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 19时30分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value = "RegionRestController", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
@Api(tags = { "地区搜索" }, produces = "application/json;charset=UTF-8")
public class RegionRestController extends BaseController {
	@Resource
	private TokenUserHelper tokenUserHelper;
	@Resource
	private IJuheCityService juheCityService;
	@Resource
	private IJuheProvinceService juheProvinceService;

	/**
	 * @author xiongxiaotun <br/>
	 * @Title: regionList <br/>
	 * @Description: 地区搜索 <br/>
	 * @param regionRequest
	 * @return
	 * @version V1.0 <br/>
	 */
	@ApiOperation(value = "地区搜索", response = RegionResponse.class, httpMethod = "POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue = "ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue = "Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String") })
	@RequestMapping("regionList")
	public ApiResponse regionList(
			@Validated @RequestBody RegionRequest regionRequest) {
		tokenUserHelper.getUser();
		JuheCityExt juheCityExt = new JuheCityExt();
		juheCityExt.setBaseWhere();
		juheCityExt.setCityName(regionRequest.getCityName());
		juheCityExt.setOrderSql("`CITY_NAME`");
		juheCityExt.setPage(regionRequest.getPage());
		juheCityExt.setRows(regionRequest.getRows());

		PageVo page = getPage(juheCityExt);
		int records = juheCityService.countByExt(juheCityExt);
		page.setRecords(records);
		if (records > 0) {
			List<JuheCity> juheCitys = juheCityService.selectByExt(juheCityExt);
			List<RegionResponse> regionResponses = mapJuheCityModels(juheCitys);
			page.setRows(regionResponses);
		} else {
			page.setRows(CollectionInitUtil.emptyList());
		}
		return ApiResponse.success(page);
	}

	// ============================================辅助方法===========================================

	/**
	 * 转换属性：展示
	 * 
	 * @param juheCitys
	 * @return
	 */
	private List<RegionResponse> mapJuheCityModels(List<JuheCity> juheCitys) {
		if (CollectionUtils.isEmpty(juheCitys)) {
			return CollectionInitUtil.emptyList();
		}
		List<RegionResponse> regionResponses = CollectionInitUtil
				.emptyList();
		for (JuheCity juheCity : juheCitys) {
			RegionResponse regionResponse = mapDpBaseCarModel(juheCity);
			if (regionResponse != null) {
				regionResponses.add(regionResponse);
			}
		}
		return regionResponses;
	}

	/**
	 * 转换属性：展示
	 * @param juheCity
	 * @return
	 */
	private RegionResponse mapDpBaseCarModel(JuheCity juheCity) {
		if (juheCity == null) {
			return null;
		}
		RegionResponse regionResponse = new RegionResponse();
		regionResponse.setCityId(juheCity.getCityId());
		regionResponse.setCityName(juheCity.getCityName());
		String proId = juheCity.getProId();
		if (StringUtils.isBlank(proId)) {
			return null;
		}
		JuheProvinceExt juheProvinceExt = new JuheProvinceExt();
		juheProvinceExt.setBaseWhere();
		juheProvinceExt.setProId(proId);
		juheProvinceExt.setRows(1);

		List<JuheProvince> juheProvinces = juheProvinceService
				.selectByExt(juheProvinceExt);
		if (CollectionUtils.isEmpty(juheProvinces)) {
			return null;
		}
		JuheProvince juheProvince = juheProvinces.get(0);
		regionResponse.setProId(juheProvince.getProId());
		regionResponse.setProName(juheProvince.getProName());
		return regionResponse;
	}
}
